Python 简介和特性
1. Python 简介
Python 是一种高级编程语言,由 Guido van Rossum 在 1989 年创造,并于 1991 年首次发布。Python 的设计哲学强调代码的可读性和简洁性,使得它成为初学者和专业开发者的理想选择。Python 的语法简洁明了,使得编写和阅读代码变得更加容易。
1.1 Python 的特性
- 易读性:Python 的语法设计使得代码看起来像普通的英语,易于理解和维护。
- 简洁性:Python 鼓励使用简洁的代码来实现复杂的功能,减少了冗余代码。
- 跨平台:Python 可以在多种操作系统上运行,包括 Windows、macOS 和 Linux。
- 丰富的标准库:Python 自带了大量的标准库,涵盖了从文件处理到网络编程的各个方面。
- 动态类型:Python 是一种动态类型语言,变量的类型在运行时确定,这使得编程更加灵活。
- 面向对象:Python 支持面向对象编程,允许开发者创建类和对象。
- 开源:Python 是开源的,拥有一个活跃的社区,提供了大量的第三方库和工具。
2. 环境搭建
2.1 安装 Python
在开始编写 Python 代码之前,首先需要安装 Python 解释器。你可以从 Python 官方网站 下载适合你操作系统的安装包。
2.1.1 Windows 安装
- 下载 Python 安装包。
- 运行安装包,勾选“Add Python to PATH”选项。
- 点击“Install Now”完成安装。
2.1.2 macOS 安装
- 下载 Python 安装包。
- 运行安装包,按照提示完成安装。
2.1.3 Linux 安装
大多数 Linux 发行版已经预装了 Python,如果没有,可以使用包管理器安装:
sudo apt-get install python3
2.2 IDE 选择
IDE(集成开发环境)可以帮助你更高效地编写和调试代码。以下是一些常用的 Python IDE:
- PyCharm:由 JetBrains 开发,功能强大,适合专业开发者。
- VS Code:轻量级且高度可定制,支持丰富的插件。
- Jupyter Notebook:适合数据科学和交互式编程。
- Sublime Text:轻量级文本编辑器,支持多种编程语言。
3. 第一个 Python 程序
让我们编写第一个 Python 程序,输出“Hello, World!”。
print("Hello, World!")
3.1 运行 Python 程序
- 打开你选择的 IDE 或文本编辑器。
- 创建一个新文件,命名为
hello.py。 - 输入上面的代码。
- 保存文件。
- 打开终端或命令提示符,导航到文件所在目录。
- 运行程序:
python3 hello.py
你应该会看到输出:
Hello, World!
4. 变量和数据类型
在 Python 中,变量是用来存储数据的容器。Python 支持多种数据类型,包括整数、浮点数、字符串、布尔值等。
4.1 变量定义
# 整数
age = 25
# 浮点数
height = 1.75
# 字符串
name = "Alice"
# 布尔值
is_student = True
4.2 数据类型
Python 提供了多种内置数据类型:
- 整数 (int):表示整数值,如
10,-5。 - 浮点数 (float):表示带小数点的数值,如
3.14,-0.001。 - 字符串 (str):表示文本数据,如
"Hello",'Python'。 - 布尔值 (bool):表示真或假,如
True,False。
5. 基本运算符
Python 支持多种基本运算符,包括算术运算符、比较运算符和逻辑运算符。
5.1 算术运算符
a = 10
b = 3
# 加法
print(a + b) # 输出: 13
# 减法
print(a - b) # 输出: 7
# 乘法
print(a * b) # 输出: 30
# 除法
print(a / b) # 输出: 3.3333333333333335
# 取整除
print(a // b) # 输出: 3
# 取余
print(a % b) # 输出: 1
# 幂运算
print(a ** b) # 输出: 1000
5.2 比较运算符
a = 10
b = 3
# 等于
print(a == b) # 输出: False
# 不等于
print(a != b) # 输出: True
# 大于
print(a > b) # 输出: True
# 小于
print(a < b) # 输出: False
# 大于等于
print(a >= b) # 输出: True
# 小于等于
print(a <= b) # 输出: False
5.3 逻辑运算符
a = True
b = False
# 与
print(a and b) # 输出: False
# 或
print(a or b) # 输出: True
# 非
print(not a) # 输出: False
6. 条件语句 (if, elif, else)
条件语句用于根据条件执行不同的代码块。
6.1 if 语句
age = 18
if age >= 18:
print("You are an adult.")
6.2 if-else 语句
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
6.3 if-elif-else 语句
age = 25
if age < 18:
print("You are a minor.")
elif age < 65:
print("You are an adult.")
else:
print("You are a senior.")
7. 循环 (for, while)
循环用于重复执行某段代码。
7.1 for 循环
# 遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 输出:
# apple
# banana
# cherry
7.2 while 循环
count = 0
while count < 5:
print(count)
count += 1
# 输出:
# 0
# 1
# 2
# 3
# 4
8. 异常处理 (try, except)
异常处理用于捕获和处理程序运行时可能出现的错误。
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
9. 列表和元组
列表和元组是 Python 中常用的数据结构,用于存储多个元素。
9.1 列表
列表是可变的,可以动态添加、删除和修改元素。
fruits = ["apple", "banana", "cherry"]
# 添加元素
fruits.append("orange")
# 删除元素
fruits.remove("banana")
# 修改元素
fruits[0] = "grape"
print(fruits) # 输出: ['grape', 'cherry', 'orange']
9.2 元组
元组是不可变的,一旦创建就不能修改。
coordinates = (10, 20)
print(coordinates[0]) # 输出: 10
print(coordinates[1]) # 输出: 20
10. 字典和集合
字典和集合是 Python 中用于存储键值对和唯一元素的数据结构。
10.1 字典
字典用于存储键值对,键必须是唯一的。
person = {
"name": "Alice",
"age": 25,
"is_student": True
}
print(person["name"]) # 输出: Alice
# 修改值
person["age"] = 26
# 添加键值对
person["city"] = "New York"
print(person) # 输出: {'name': 'Alice', 'age': 26, 'is_student': True, 'city': 'New York'}
10.2 集合
集合用于存储唯一元素,不支持重复值。
fruits = {"apple", "banana", "cherry"}
# 添加元素
fruits.add("orange")
# 删除元素
fruits.remove("banana")
print(fruits) # 输出: {'apple', 'cherry', 'orange'}
11. 字符串操作
字符串是 Python 中常用的数据类型,支持多种操作。
11.1 字符串拼接
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # 输出: Alice Smith
11.2 字符串格式化
name = "Alice"
age = 25
message = f"My name is {name} and I am {age} years old."
print(message) # 输出: My name is Alice and I am 25 years old.
12. 函数定义和调用
函数是组织代码的基本单元,用于封装可重用的代码块。
12.1 定义函数
def greet(name):
print(f"Hello, {name}!")
# 调用函数
greet("Alice") # 输出: Hello, Alice!
12.2 返回值
函数可以返回一个值,供调用者使用。
def add(a, b):
return a + b
result = add(3, 5)
print(result) # 输出: 8
13. 参数和返回值
函数可以接受参数,并返回一个值。
13.1 参数
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")
greet("Alice", 25) # 输出: Hello, Alice! You are 25 years old.
13.2 默认参数
def greet(name, age=18):
print(f"Hello, {name}! You are {age} years old.")
greet("Alice") # 输出: Hello, Alice! You are 18 years old.
greet("Bob", 30) # 输出: Hello, Bob! You are 30 years old.
14. 模块导入和使用
模块是 Python 中组织代码的方式,可以将代码分成多个文件。
14.1 导入模块
import math
result = math.sqrt(16)
print(result) # 输出: 4.0
14.2 导入特定函数
from math import sqrt
result = sqrt(16)
print(result) # 输出: 4.0
15. 包管理 (pip)
pip 是 Python 的包管理工具,用于安装和管理第三方库。
15.1 安装包
pip install requests
15.2 使用包
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 输出: 200
16. 类和对象
类是面向对象编程的基础,用于创建对象。
16.1 定义类
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} is barking!")
# 创建对象
my_dog = Dog("Buddy", 3)
my_dog.bark() # 输出: Buddy is barking!
16.2 继承
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} is speaking!")
class Dog(Animal):
def bark(self):
print(f"{self.name} is barking!")
my_dog = Dog("Buddy")
my_dog.speak() # 输出: Buddy is speaking!
my_dog.bark() # 输出: Buddy is barking!
17. 封装和抽象
封装和抽象是面向对象编程的重要概念。
17.1 封装
封装用于隐藏对象的内部细节,只暴露必要的接口。
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient funds!")
def get_balance(self):
return self.__balance
account = BankAccount(1000)
account.deposit(500)
account.withdraw(200)
print(account.get_balance()) # 输出: 1300
17.2 抽象
抽象用于定义接口,而不实现具体的功能。
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def speak(self):
pass
class Dog(Animal):
def speak(self):
print("Woof!")
class Cat(Animal):
def speak(self):
print("Meow!")
my_dog = Dog()
my_dog.speak() # 输出: Woof!
my_cat = Cat()
my_cat.speak() # 输出: Meow!
18. 读写文本文件
Python 提供了简单的方式来读写文本文件。
18.1 写文件
with open("example.txt", "w") as file:
file.write("Hello, World!")
18.2 读文件
with open("example.txt", "r") as file:
content = file.read()
print(content) # 输出: Hello, World!
19. 处理 CSV, JSON 数据
Python 提供了内置的库来处理 CSV 和 JSON 数据。
19.1 处理 CSV
import csv
# 写 CSV 文件
with open("example.csv", "w", newline='') as file:
writer = csv.writer(file)
writer.writerow(["Name", "Age"])
writer.writerow(["Alice", 25])
writer.writerow(["Bob", 30])
# 读 CSV 文件
with open("example.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
19.2 处理 JSON
import json
# 写 JSON 文件
data = {
"name": "Alice",
"age": 25,
"is_student": True
}
with open("example.json", "w") as file:
json.dump(data, file)
# 读 JSON 文件